package com.donger.business.app.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.app.entity.AppVersion;
import com.donger.business.app.query.AppVersionQuery;
import com.donger.business.app.query.CheckAppVersion;
import com.donger.business.app.service.AppVersionService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import com.donger.common.core.validator.ValidatorUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 7326
 */
@RequestMapping("/app/version")
@RestController
public class VersionController {

    @Autowired
    private AppVersionService appVersionService;

    @GetMapping("/page")
    public Result page(Page<AppVersion> page, AppVersionQuery query) {
        return Res.ok(appVersionService.page(page, Wrappers.<AppVersion>lambdaQuery()
                .like(StrUtil.isNotBlank(query.getVersion()), AppVersion::getVersion, query.getVersion())
                .orderByDesc(AppVersion::getBuild)
        ));
    }


    @PostMapping("/save")
    public Result save(@RequestBody AppVersion appVersion) {
        ValidatorUtil.validateEntity(appVersion);
        appVersion.setCreateTime(LocalDateTime.now());
        appVersion.setUpdateTime(LocalDateTime.now());
        appVersionService.save(appVersion);
        return Res.ok();
    }


    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Long id) {
        appVersionService.removeById(id);
        return Res.ok();
    }


    @ApiOperation(value = "检测App是否有更新")
    @GetMapping("/check")
    public Result check(@RequestParam("buildBuildVersion")
                        @ApiParam("buildBuildVersion")
                                Integer buildBuildVersion) {
        List<AppVersion> list = appVersionService.list(Wrappers.<AppVersion>lambdaQuery()
                .orderByDesc(AppVersion::getBuild)
                .last("limit 1")
        );
        AppVersion appVersion = null;
        // 如果没有  有数据
        CheckAppVersion checkAppVersion = new CheckAppVersion();
        if (CollUtil.isNotEmpty(list)) {
            appVersion = list.get(0);
            // 有新版本
            if (buildBuildVersion > appVersion.getBuild()) {
                checkAppVersion.setBuildHaveNewVersion(Boolean.TRUE);
            }
            checkAppVersion.setBuildUpdateDescription(appVersion.getDescription());
            checkAppVersion.setBuildVersion(appVersion.getBuild());
            checkAppVersion.setDownloadUrl(appVersion.getUrl());
            checkAppVersion.setVersion(appVersion.getVersion());
            return Res.ok(checkAppVersion);
        } else {
            return Res.ok("没有上传APP");
        }
    }


}
